'use strict';
/* jshint eqeqeq: false, unused: false, expr: true */

angular.module('openshiftConsole')
.factory('MyDataService', function($cacheFactory, $http, $ws, $rootScope, $q, API_CFG, APIService, Notification, Logger, $timeout) {

  function MyDataService() {

    var self = this;

  }

  MyDataService.prototype.getLog = function(user) {
    var deferred = $q.defer();
    var url = 'https://api.hs.com:442/logs';

    $http({
      method: 'GET',
      url:url,
      params:{'user':user}
    }).success(function(data) {
        deferred.resolve(data);
      })
      .error(function(data, status, headers, config) {
        deferred.reject({
          data: data,
          status: status,
          headers: headers,
          config: config
        });
      });
    return deferred.promise;
  };
  MyDataService.prototype.setLog = function(user,detail,level) {
    var deferred = $q.defer();
    var url = 'https://api.hs.com:442/logs';
    $http({
      method: 'POST',
      url:url,
      params:{'user':user,'detail':detail,'level':level}
    }).success(function(data) {
        deferred.resolve(data);
      })
      .error(function(data, status, headers, config) {
        deferred.reject({
          data: data,
          status: status,
          headers: headers,
          config: config
        });
      });
    return deferred.promise;
  };

  //
  //MyDataService.prototype.update = function(resource, name, object, context, opts) {
  //  resource = APIService.deriveTargetResource(resource, object);
  //  opts = opts || {};
  //  var deferred = $q.defer();
  //  var self = this;
  //  this._getNamespace(resource, context, opts).then(function(ns){
  //    $http(angular.extend({
  //      method: 'PUT',
  //      auth: {},
  //      data: object,
  //      url: self._urlForResource(resource, name, context, false, ns)
  //    }, opts.http || {}))
  //    .success(function(data, status, headerFunc, config, statusText) {
  //      deferred.resolve(data);
  //    })
  //    .error(function(data, status, headers, config) {
  //      deferred.reject({
  //        data: data,
  //        status: status,
  //        headers: headers,
  //        config: config
  //      });
  //    });
  //  });
  //  return deferred.promise;
  //};
  //
  //MyDataService.prototype.create = function(resource, name, object, context, opts) {
  //  resource = APIService.deriveTargetResource(resource, object);
  //  opts = opts || {};
  //  var deferred = $q.defer();
  //  var self = this;
  //
  //  this._getNamespace(resource, context, opts).then(function(ns){
  //    console.log(self._urlForResource(resource, name, context, false, ns));
  //    $http(angular.extend({
  //      method: 'POST',
  //      auth: {},
  //      data: object,
  //      url: self._urlForResource(resource, name, context, false, ns)
  //    }, opts.http || {}))
  //    .success(function(data, status, headerFunc, config, statusText) {
  //      deferred.resolve(data);
  //    })
  //    .error(function(data, status, headers, config) {
  //      deferred.reject({
  //        data: data,
  //        status: status,
  //        headers: headers,
  //        config: config
  //      });
  //    });
  //  });
  //  return deferred.promise;
  //};


  //
  //MyDataService.prototype.watch = function(resource, context, callback, opts) {
  //  resource = APIService.toResourceGroupVersion(resource);
  //  opts = opts || {};
  //  var key = this._uniqueKey(resource, null, context, _.get(opts, 'http.params'));
  //  if (callback) {
  //    // If we were given a callback, add it
  //    this._watchCallbacks(key).add(callback);
  //  }
  //  else if (!this._watchCallbacks(key).has()) {
  //    // We can be called with no callback in order to re-run a list/watch sequence for existing callbacks
  //    // If there are no existing callbacks, return
  //    return {};
  //  }
  //
  //  var existingWatchOpts = this._watchOptions(key);
  //  if (existingWatchOpts) {
  //    // Check any options for compatibility with existing watch
  //    if (existingWatchOpts.poll != opts.poll) {
  //      throw "A watch already exists for " + resource + " with a different polling option.";
  //    }
  //  }
  //  else {
  //    this._watchOptions(key, opts);
  //  }
  //
  //  var self = this;
  //  if (this._isCached(key)) {
  //    if (callback) {
  //      $timeout(function() {
  //        callback(self._data(key));
  //      }, 0);
  //    }
  //  }
  //  else {
  //    if (callback) {
  //      var existingData = this._data(key);
  //      if (existingData) {
  //        $timeout(function() {
  //          callback(existingData);
  //        }, 0);
  //      }
  //    }
  //    if (!this._listInFlight(key)) {
  //      this._startListOp(resource, context, opts);
  //    }
  //  }
  //
  //  // returned handle needs resource, context, and callback in order to unwatch
  //  return {
  //    resource: resource,
  //    context: context,
  //    callback: callback,
  //    opts: opts
  //  };
  //};
  //

  //
  //MyDataService.prototype.watchObject = function(resource, name, context, callback, opts) {
  //  resource = APIService.toResourceGroupVersion(resource);
  //  opts = opts || {};
  //  var key = this._uniqueKey(resource, name, context, _.get(opts, 'http.params'));
  //  var wrapperCallback;
  //  if (callback) {
  //    // If we were given a callback, add it
  //    this._watchObjectCallbacks(key).add(callback);
  //    var self = this;
  //    wrapperCallback = function(items, event, item) {
  //      // If we got an event for a single item, only fire the callback if its the item we care about
  //      if (item && item.metadata.name === name) {
  //        self._watchObjectCallbacks(key).fire(item, event);
  //      }
  //      else if (!item) {
  //        // Otherwise its an initial listing, see if we can find the item we care about in the list
  //        var itemsByName = items.by("metadata.name");
  //        if (itemsByName[name]) {
  //          self._watchObjectCallbacks(key).fire(itemsByName[name]);
  //        }
  //      }
  //    };
  //  }
  //  else if (!this._watchObjectCallbacks(key).has()) {
  //    // This block may not be needed yet, don't expect this would get called without a callback currently...
  //    return {};
  //  }
  //
  //  // For now just watch the type, eventually we may want to do something more complicated
  //  // and watch just the object if the type is not already being watched
  //  var handle = this.watch(resource, context, wrapperCallback, opts);
  //  handle.objectCallback = callback;
  //  handle.objectName = name;
  //
  //  return handle;
  //};




  return new MyDataService();
});
